package com.stx.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.stx.domain.*;
import com.stx.service.AdminService;
import com.stx.service.BuildService;
import com.stx.service.CeService;
import com.stx.service.CuService;
import com.stx.service.DormService;
import com.stx.service.MaintainingService;
import com.stx.service.StudentService;
import com.stx.service.TeacherService;

@Controller
//@RequestMapping("StudentController")
public class StudentController {
	@Resource
	private AdminService as;
	@Resource
	private BuildService bs;
	@Resource
	private CeService ces;
	@Resource
	private CuService cus;
	@Resource
	private DormService ds;
	@Resource
	private StudentService ss;
	@Resource
	private MaintainingService mis;
	@Resource
	private TeacherService ts;

	private List list=new ArrayList();
	
	private static final Log logger = LogFactory.getLog(StudentController.class);
	/**
	 * 登录
	 */
//	@RequestMapping(value="studentLogin.do",method = RequestMethod.GET)
//	public String studentLogin(){
//		logger.info("GET studentLogin called");
//		return "student/loginStudent";
//	}	
	@RequestMapping(value="studentLogin.do",method = RequestMethod.POST)
	public String studentLogin(int no,String pwd,HttpSession session,Model model,RedirectAttributes redirectAttributes){
		logger.info("post studentLogin called");
		Student student=null;
		try {//捕获dao层关闭连接和结果集的异常
			student=ss.studentLogin(no, pwd);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(student!=null){
			session.setAttribute("student", student);
			return "student/menuStudent";
		}
		else{
			redirectAttributes.addFlashAttribute("msg", "登录失败！");
			return "redirect:index.jsp";
		}
			
	}
	
	/**
	 * 查询缺勤
	 * 
	 */
	@RequestMapping(value="selectCe.do",method = RequestMethod.GET)
	public String selectCe(Model model, int no){
		logger.info("GET selectCe called");
		try {
			list=ces.selectAbsentByNo(no);
		} catch (Exception e) {
			e.printStackTrace();
		}
		model.addAttribute("list",list);
		return "student/selectCe";
		
	}
	
	/**
	 * 修改密码
	 * 
	 */
	@RequestMapping(value="modifyPassword.do",method = RequestMethod.GET)
	public String modifyPassword(Model model){
		logger.info("GET modifyPassword called");
		return "student/passwordStudent";	
	}
	@RequestMapping(value="modifyPassword.do",method = RequestMethod.POST)
	public String modifyPassword(Model model,String oldpassword,String newpassword,HttpSession session){
		logger.info("POST modifyPassword called");
		Student student=(Student) session.getAttribute("student");
		int no=0; String pwd="";
		if(student!=null){
			no=student.getNo();
			pwd=student.getPwd();
		} else{
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
		}
		if(pwd.equals(oldpassword)){
			int i=0;
			try {
				i=ss.modifyStudentPwd(no, newpassword);
			} catch (Exception e) {
				e.printStackTrace();
			}
			if(i==1){
				//System.out.println(session.getServletContext().getContextPath());
				//String path=session.getServletContext().getContextPath();
				return "student/passwordSuccess";//修改成功去主页登录,因为不经过服务器，所以要加上.jsp后缀。
			} else{
				model.addAttribute("msg", "修改失败！");
				return "student/passwordStudent";
			}
		}
		else {
			model.addAttribute("msg", "旧密码错误");
			return "student/passwordStudent";
		}
	}
/*	*//**
	 * 学生注册
	 *//*
	@RequestMapping(value="registStudent.do",method = RequestMethod.GET)
	public String registerStudent(){
		logger.info("GET registStudent called");
		return "student/registStudent";	
	}
	@RequestMapping(value="registStudent.do",method = RequestMethod.POST)
	public String registStudent(){
		logger.info("POST registStudent called");
		return "student/registerNo";	
	}*/
//报修
	@RequestMapping(value="addMaintaining.do",method = RequestMethod.GET)
	public String addMaintaining(){
		logger.info("GET addMaintaining called");
		
		return "student/addMaintaining";
		
	}
	
	@RequestMapping(value="addMaintaining.do",method = RequestMethod.POST)
	public String addMaintaining(Maintaining main){
		logger.info("post addMaintaining called");

		mis.addMaintaining(main);
		
		return "student/addMaintaining";
		
	}
//查看上报修信息
	@RequestMapping(value="selectMaintaining.do",method = RequestMethod.GET)
	public String selectMaintaining(Model model, HttpSession session){
		logger.info("GET selectMaintaining called");

	Student student=	(Student) session.getAttribute("student");
		list=mis.selectMaintaining(student.getD());
		
		model.addAttribute("list",list);
		return "student/selectMaintaining";
	}
	@RequestMapping(value="deleteMaintaining.do",method = RequestMethod.GET)
	public String deleteMaintaining(Model model,int id){
		logger.info("GET deleteMaintaining called");

		mis.deleteMaintaining(id);
		
		return "redirect:selectMaintaining.do";
	}
}
